<template>
  <ul>
    <li v-for="item in menuData" :key="item.id">
      <div>
        {{ item.label }}
      </div>
      <MenuList v-if="item.children && item.expanded" :menuData="item.children" />
    </li>
  </ul>
</template>

<script setup lang="ts">
import { ref } from 'vue'

interface MenuItem {
  id: number
  label: string
  children?: MenuItem[]
  expanded?: boolean
}

const props = defineProps<{
  menuData: MenuItem[]
}>()

// 在 menuData 初始化时，确保默认展开每个有子级的菜单项
props.menuData.forEach((item) => {
  if (item.children) {
    item.expanded = true // 设置默认展开
    item.children.forEach((child) => {
      if (child.children) {
        child.expanded = true // 如果有子级，再次设置默认展开
      }
    })
  }
})
</script>

<style scoped>
ul {
  list-style-type: none;
  padding-left: 20px;
}

li {
  margin: 5px 0;
  cursor: pointer;
}

li div {
  padding: 5px;
  border-radius: 4px;
}

li div:hover {
  background-color: #e0e0e0;
}
</style>
